package me.numark.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import me.numark.R;
import me.numark.activities.ProfileActivity;
import me.numark.fragments.FriendsFragment;
import me.numark.model.User;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.LogUtils;

/**
 * Created by ytian on 15/8/1.
 */
public class UserListViewHolder extends BaseViewHolder implements View.OnClickListener {
    Context mContext;

    CircleImageView avatar;

    TextView name;

    ImageView source;

    TextView info;

    TextView signature;

    ImageView followStatus;

    User mUser;

    FriendsFragment mFragment;

    RelativeLayout userItem;


    public static final int TYPE_NONE = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_FRIST_ADD =2;
    int mType;
    int mUserId;
    public UserListViewHolder(View itemView, Object parent, int type,int userId) {
        super(itemView, parent);
        mUserId = userId;
        mContext = itemView.getContext();
        mType = type;
        if (mType == TYPE_NONE)
            return;
        avatar = ButterKnife.findById(itemView, R.id.avatar);
        avatar.setBorderWidth(2);
        avatar.setBorderColor(Global.avatarColor);
        name = ButterKnife.findById(itemView, R.id.name);
        source = ButterKnife.findById(itemView, R.id.source);
        info = ButterKnife.findById(itemView, R.id.info);
        signature = ButterKnife.findById(itemView, R.id.signature);
        followStatus = ButterKnife.findById(itemView, R.id.follow_status);
        userItem = ButterKnife.findById(itemView,R.id.user_item);
        addOnClickListener(followStatus);
        addOnClickListener(avatar);
        addOnClickListener(userItem);
        if (parent instanceof FriendsFragment) {
            mFragment = (FriendsFragment) parent;
            if (mFragment.type != -1 && source != null) {
                source.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        final User user = (User) v.getTag();
        switch (v.getId()) {
            case R.id.follow_status:

                if (mFragment != null) {
                    if (mType == TYPE_FRIST_ADD) {
                        if (mFragment.mCheckedUsers.contains(user.uid)) {
                            mFragment.mCheckedUsers.remove(user.uid);
                            user.is_follow = !user.is_follow;
                        }else{
                            mFragment.mCheckedUsers.add(user.uid);
                            user.is_follow = !user.is_follow;
                        }
                        mFragment.mUsersListAdapter.notifyDataSetChanged();
                    }else {

                        new RequestServerTask<BaseResponse>(BaseResponse.class) {
                            @Override
                            protected String requestServer() {
                                String baseUrl = user.is_follow ? NetworkConstants.UNFOLLOW_URL : NetworkConstants.FOLLOW_URL;
                                String url = String.format(baseUrl, mUserId, user.uid);
                                return HttpUtil.get(url, null);
                            }

                            @Override
                            protected void onSuccess(BaseResponse result) {
                                user.is_follow = !user.is_follow;
                                mFragment.mUsersListAdapter.notifyDataSetChanged();
                            }
                        }.start();
                    }
                }
                break;
            case R.id.avatar:
            case R.id.user_item:
                ProfileActivity.openProfile(mContext, user.uid);
                break;
        }
    }

    public void bindData(User user) {
        mUser = user;

        if (!TextUtils.isEmpty(mUser.pic)) {
            Picasso.with(mContext).load(mUser.pic).into(avatar);
        } else {
            avatar.setImageResource(R.drawable.avatar_bg_normal);
        }
        name.setText(mUser.nick);
        info.setText(mContext.getString(R.string.friend_info, mUser.fans, mUser.follow));
        signature.setText(mUser.des);
        source.setImageResource(mUser.getFromTypeDrawable());
        //LogUtils.d("bindData, user id is " + user.uid + ", checked? " + mFragment.mCheckedUsers.contains(user.uid));
        if (mFragment != null) {
            if (mType == TYPE_FRIST_ADD) {
                //if (mFragment.mCheckedUsers.contains(user.uid)) {
                    followStatus.setImageResource(user.is_follow ? R.drawable.follow_follow : R.drawable.follow_followed);
                /*} else {
                    followStatus.setImageResource(user.getFollowStatusDrawable());
                }*/
            }else {
                followStatus.setImageResource(user.getFollowStatusDrawable());
            }
        }
        avatar.setTag(user);
        avatar.setOnClickListener(this);
        userItem.setTag(user);
        userItem.setOnClickListener(this);
        followStatus.setTag(user);
        followStatus.setOnClickListener(this);
    }
}
